﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.ScheduleJob.Manager;
using T.FAS.Runtime.ScheduleJob.Service;
using T.FAS.Runtime.ScheduleJob.SGSPI;
using T.FAS.Runtime.ScheduleJob.SPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.ScheduleJob.SGPlugin
{
    [SGService]
    public class ScheduleJobSGService : IScheduleJobSGService
    {
        [SGService(ServiceID = "FASR-ResumeAllJobs")]
        public void ResumeAllJobs()
        {
            var service = ScheduleJobService.GetInstance();
            service.ResumeAllJobs();
        }
        public void DeleteJobInstance(string instanceID)
        {
            var service = ScheduleJobService.GetInstance();
            service.DeleteJob(instanceID);
        }
        public void StartJobInstance(string jobID)
        {
            var service = ScheduleJobService.GetInstance();
            var instance = service.CreateScheduleJobInstance(jobID);
            service.StartJob(instance);
        }

        public void PauseJobInstance(string instanceID)
        {
            var service = ScheduleJobService.GetInstance();
            service.PauseJob(instanceID);
        }

        public void ResumeJobInstance(string instanceID)
        {
            var service = ScheduleJobService.GetInstance();
            service.ResumeJob(instanceID);
        }

        public List<SPI.ScheduleJobInstanceStore> GetAllActiveJobs()
        {
            var service = ScheduleJobControlService.GetInstance();
            return service.GetAllActiveJobs();
        }
        public ScheduleJobExecuteInfo GetScheduleJobExecuteInfo(string instanceID)
        {
            var service = ScheduleJobControlService.GetInstance();
            var list = service.GetScheduleJobExecuteInfos(new List<string>() { instanceID });
            if (list == null || list.Count <= 0)
                return null;
            return list[0];
        }
        public Dictionary<string, string> CreateScheduleJob(Metadata.Entity.ScheduleJob.ScheduleJob scheduleJob)
        {
            var res = new Dictionary<string, string>();
            try
            {
                if (scheduleJob == null)
                    throw new Exception("scheduleJob不能为空");
                if (scheduleJob.ScheduleTrigger == null || string.IsNullOrEmpty(scheduleJob.ScheduleTrigger.CronExpression))
                    throw new Exception("Cron不能为空");
                var instance = new ScheduleJobInstance();
                instance.ScheduleJobID = scheduleJob.ID;
                instance.ScheduleJobGroup = scheduleJob.ScheduleGroup;
                instance.UserID = UserUtil.GetCurrentUserID();
                instance.UserName = UserUtil.GetCurrentUserName();
                instance.ScheduleJobID = scheduleJob.ID;

                scheduleJob.Ext1 = "userdefine";
                scheduleJob.ScheduleTrigger.ID = Guid.NewGuid().ToString();
                scheduleJob.ScheduleTrigger.Code = scheduleJob.Code + "Trigger";
                scheduleJob.ScheduleTrigger.Name = scheduleJob.Name + "Trigger";
                scheduleJob.ScheduleTrigger.Description = scheduleJob.Description;
                scheduleJob.ScheduleTrigger.TriggerGroup = scheduleJob.ScheduleGroup;
                scheduleJob.ScheduleTrigger.RepeatCount = 3;
                scheduleJob.ScheduleTrigger.RepeatInterval = 5;
                scheduleJob.ScheduleTrigger.Priority = 3;
                scheduleJob.ScheduleTrigger.EffectiveTime = DateTime.Now;
                scheduleJob.ScheduleTrigger.ExpiredTime = DateTime.Now.AddYears(10);
                scheduleJob.ScheduleTrigger.MisfirePolicy = Metadata.Entity.ScheduleJob.MisfirePolicy.Schedule_IgnoreMisfire;

                instance.ScheduleJobMetadata = scheduleJob;
                instance.Data = new ScheduleJobDataMap();
                instance.Data.AddJsonData("{}");
                instance = ScheduleJobService.GetInstance().StartJob(instance);
                res.Add("state", "1");
                res.Add("instanceId", instance.ID);
            }
            catch (Exception e)
            {
                res.Add("state", "0");
                res.Add("errmsg", e.ToString());
            }
            return res;
        }
    }
}
